package com.mmall.seckill.common.redis;

import com.mmall.seckill.common.redis.keyCode.KeyPrefix;
import com.mmall.seckill.common.redis.keyCode.UserKey;
import com.mmall.seckill.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;


/**
 * Created by megan on 2018/6/16.
 * 提供redis服务
 */
@Slf4j
@Service
public class RedisService {

    @Resource
    private JedisPoolFactory jedisPoolFactory;

    /**
     * 删除del
     * @param key key
     * @return result
     */
    public  Long del(KeyPrefix keyPrefix,String key){
        Jedis jedis = null;
        Long result = null;
        try {
            jedis = jedisPoolFactory.getJedis();
            result = jedis.del(keyPrefix.getPrefix() + key);
        } catch (Exception e) {
            log.error("redis del key:{} error ",key,e);
            jedisPoolFactory.returnBrokenResource(jedis);
            return result;
        }
        jedisPoolFactory.returnResource(jedis);
        return result;
    }

    /**
     * 增加一个值
     * @param keyPrefix
     * @param key
     * @return
     */
    public  Long incr(KeyPrefix keyPrefix,String key){
        Jedis jedis = null;
        Long result = null;
        try {
            jedis = jedisPoolFactory.getJedis();
            result = jedis.incr(keyPrefix.getPrefix() + key);
        } catch (Exception e) {
            log.error("redis del key:{} error ",key,e);
            jedisPoolFactory.returnBrokenResource(jedis);
            return result;
        }
        jedisPoolFactory.returnResource(jedis);
        return result;
    }

    /**
     * 减少一个值
     * @param keyPrefix
     * @param key
     * @return
     */
    public  Long decr(KeyPrefix keyPrefix,String key){
        Jedis jedis = null;
        Long result = null;
        try {
            jedis = jedisPoolFactory.getJedis();
            result = jedis.decr(keyPrefix.getPrefix() + key);
        } catch (Exception e) {
            log.error("redis del key:{} error ",key,e);
            jedisPoolFactory.returnBrokenResource(jedis);
            return result;
        }
        jedisPoolFactory.returnResource(jedis);
        return result;
    }

    /**
     * 设置key的有效期
     * @param key key
     * @param extime 有效期(秒)
     * @return result 超时(1),没有设置(0)
     */
    public  Long expire(KeyPrefix keyPrefix,String key,int extime){
        Jedis jedis = null;
        Long result = null;
        try {
            jedis = jedisPoolFactory.getJedis();
            result = jedis.expire(keyPrefix.getPrefix() + key,extime);
        } catch (Exception e) {
            log.error("redis expire key:{} extime:{} error ",key,e);
            jedisPoolFactory.returnBrokenResource(jedis);
            return result;
        }
        jedisPoolFactory.returnResource(jedis);
        return result;
    }

    /**
     * setEx 创建并设置有效期
     * @param key key
     * @param vlaue vlaue
     * @param extime 超时时间(秒)
     * @return  result
     */
    public  String setEx(KeyPrefix keyPrefix,String key,String vlaue,int extime){
        Jedis jedis = null;
        String result = null;
        try {
            jedis = jedisPoolFactory.getJedis();
            result = jedis.setex(keyPrefix.getPrefix() + key,extime,vlaue);
        } catch (Exception e) {
            log.error("redis setEx key:{} value:{} error ",key,vlaue,e);
            jedisPoolFactory.returnBrokenResource(jedis);
            return result;
        }
        jedisPoolFactory.returnResource(jedis);
        return result;
    }


    /**
     * 设置对象
     * */
    public <T> boolean setByT(KeyPrefix prefix, String key, T value) {
        Jedis jedis = null;
        try {
            jedis =  jedisPoolFactory.getJedis();
            String str = JsonUtil.obj2String(value);
            if(str == null || str.length() <= 0) {
                return false;
            }
            //生成真正的key
            String realKey  = prefix.getPrefix() + key;
            int seconds =  prefix.expireSeconds();
            if(seconds <= 0) {
                jedis.set(realKey, str);
            }else {
                jedis.setex(realKey, seconds, str);
            }
            return true;
        }catch (Exception e){
            log.error("redis set key:{} value:{} error ",key,value,e);
            jedisPoolFactory.returnBrokenResource(jedis);
            return false;
        }finally {
            jedisPoolFactory.returnResource(jedis);
        }
    }


    public  String setEx(KeyPrefix prefix,String key,String vlaue){
        Jedis jedis = null;
        String result = null;
        try {
            jedis = jedisPoolFactory.getJedis();
            String keys = prefix.getPrefix() + key;
            int ex = prefix.expireSeconds();
            result = jedis.setex(keys,ex,vlaue);
        } catch (Exception e) {
            log.error("redis setex key:{} value:{} error ",key,vlaue,e);
            jedisPoolFactory.returnBrokenResource(jedis);
            return result;
        }
        jedisPoolFactory.returnResource(jedis);
        return result;
    }

    /**
     * set
     * @param key key
     * @param vlaue vlaue
     * @return result
     */
    public  String set(KeyPrefix prefix,String key,String vlaue){
        Jedis jedis = null;
        String result = null;
        try {
            jedis = jedisPoolFactory.getJedis();
            result = jedis.set(prefix.getPrefix() + key,vlaue);
        } catch (Exception e) {
            log.error("redis set key:{} value:{} error ",key,vlaue,e);
            jedisPoolFactory.returnBrokenResource(jedis);
            return result;
        }
        jedisPoolFactory.returnResource(jedis);
        return result;
    }

    /**
     * get key
     * @param prefix
     * @param key
     * @return
     */
    public String get(KeyPrefix prefix,String key){
        Jedis jedis = null;
        String result = null;
        try {
            jedis = jedisPoolFactory.getJedis();
            result = jedis.get(prefix.getPrefix() + key);
        } catch (Exception e) {
            log.error("redis get key:{} value:{} error ", prefix.getPrefix() + key,e);
            jedisPoolFactory.returnBrokenResource(jedis);
            return result;
        }
        jedisPoolFactory.returnResource(jedis);
        return result;
    }


    public <T> T get(KeyPrefix prefix, String key,  Class<T> clazz) {
        Jedis jedis = null;
        try {
            jedis =  jedisPoolFactory.getJedis();
            //生成真正的key
            String realKey  = prefix.getPrefix() + key;
            String  str = jedis.get(realKey);
            T t =  JsonUtil.string2Obj(str, clazz);
            return t;
        }finally {
            jedisPoolFactory.returnResource(jedis);
        }
    }


    public static void main(String[] args) {
        log.info("UserKeyId -> {}",UserKey.getById);
    }

}
